﻿using System;
using System.Globalization;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Microsoft.Win32;
using Microsoft.MediaCenter;
using Microsoft.MediaCenter.UI;

namespace McePicasa
{
    class VersionManager
    {
        private VersionManager() { } //For FxCop only

        public static void CheckForNewVersion(string serverText, string serverText1)
        {
            string NewReleaseLink = "https://www.codeplex.com/Release/ProjectReleases.aspx?ProjectName=McePicasa";
            //Local version is stored in registry, update in Setup.wxs (UPDATE THIS FILE FOR EVERY RELEASE!!!)
            //Server version is the last 3 chars of the Summary field of the first album of the Sample album:)
            //Sample album infor: 999wonders/f(213)

            if (serverText == null || serverText.Length < 3) return;
            Application.Current.MyVersion = Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Microsoft\McePicasa", @"Version", null) as string;
            if (Application.Current.MyVersion == null)
                Application.Current.MyVersion = Registry.GetValue(@"HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\McePicasa", @"Version", null) as string;
            //if (Application.Current.MyVersion == null || Application.Current.MyVersion.Length < 3) return;

            //Check for new version every five days.
            DateTime checkVersionDate;
            try
            {
                checkVersionDate =
                    DateTime.Parse(Properties.Settings.Default.CheckVersionDate, CultureInfo.CurrentCulture);
                if (checkVersionDate.AddDays(5).CompareTo(DateTime.Now) > 0) return;
            }
            catch (FormatException)
            {
                Log.LogIt("Cannot get saved CheckVersionDate!!!");
            }                

            Application.Current.NewVersion = serverText.Substring(serverText.Length - 3);
            //Application.Current.MessageBox(Application.Current.MyVersion, Application.Current.NewVersion);

            if ((Application.Current.MyVersion == null) || 
                (String.Compare(Application.Current.MyVersion, Application.Current.NewVersion, StringComparison.CurrentCultureIgnoreCase) < 0))
            {
                Properties.Settings.Default.CheckVersionDate =
                    DateTime.Now.ToString(CultureInfo.CurrentCulture);
                Properties.Settings.Default.Save();

                DialogResult r = Application.Current.MediaCenterEnvironment.Dialog(
                    "There is a new version of McePicasa, do you want to get more information?", 
                    "McePicasa",
                    DialogButtons.Yes | DialogButtons.No, 20, true);
                if (r == DialogResult.Yes)
                {
                    if (serverText1.StartsWith("http", StringComparison.CurrentCultureIgnoreCase)) NewReleaseLink = serverText1;
                    try
                    {
                        //We might want to open the page within MCE:)
                        Process p = new Process();
                        p.StartInfo.FileName = "iexplore.exe";
                        p.StartInfo.Arguments = NewReleaseLink;
                        p.Start();
                        //Application.Current.MediaCenterEnvironment.MediaExperience.GoToFullScreen
                        //We need to put MCE Windows to minimized!
                    }
                    catch (System.ComponentModel.Win32Exception e)
                    {
                        Application.Current.MessageBox("Please go to " +
                            NewReleaseLink + " for more details", e.Message, 20, false);
                    }
                    catch
                    {
                        throw;
                    }
                }
            }
        }
    }
}
